require(['grid', 'topBar', 'layout'], function () {

    var dialog;
    $('body').layout();
    $('#top').topBar({
        left: [
            {
                text: '查询',
                type: 'btn',
                click: function () {
                	search();
                }
            },
            {
                text: '设置',
                type: 'btn',
                click: function () {
	                	var row= $("#grid").getGridParam("reccount")
						var rowIds = $("#grid").jqGrid("getGridParam", "selarrrow");
						if (rowIds.length < 1) {
							$.messager.alert("提示","请至少选择一个设备");
							return false;
						}
						var eqmtIdArr=[];
						var eqmtCdArr=[];
						var vrsnIdArr=[];
						var updateVersonArr=[];
						var carIdArr=[];
						var temp;
						for(var i = 0; i < rowIds.length; i ++){
							var rowData = $("#grid").getRowData(rowIds[i]);
							vrsnId=$("#grid select:eq("+(rowIds[i]-1)+")").val();
							if(vrsnId!=""){
								tempName=$("#grid").find("option:selected:eq("+(rowIds[i]-1)+")").text()
								eqmtIdArr.push(rowData.eqmtId);
								eqmtCdArr.push(rowData.eqmtCd);
								vrsnIdArr.push(vrsnId);
								updateVersonArr.push(tempName);
								carIdArr.push(rowData.carId);
							}else{
								$.messager.alert("警告","	请选择设置版本名称!","warning");
								return false;
							}
						}
						var params = [];
						params.push({
							name : "eqmtIdArr",
							value : eqmtIdArr
						},{
							name : "vrsnIdArr",
							value : vrsnIdArr
						},{
							name : "updateVersonArr",
							value : updateVersonArr
						},{
							name : "carIdArr",
							value : carIdArr
						},{
							name : "eqmtCdArr",
							value : eqmtCdArr
						});
						$.ajaxExt({
							url : "/airConditioner/airTemplateMonitorUpgrade.htm",
							data :{
								"eqmtIdArr":eqmtIdArr,
								"vrsnIdArr":vrsnIdArr,
								"updateVersonArr":updateVersonArr,
								"carIdArr":carIdArr,
								"eqmtCdArr":eqmtCdArr								
							}
						}).done(function(result){
							search();
						});
					}
                }
        ]
    });

    $('#grid').grid({
    	url : "/airConditioner/searchAirTemplateMonitor.htm",
        colModel: [
		    {name : "rcrdId", label:"id",hidden : true},
			{name : "licPltNo", label:"车牌号"},
			{name : "eqmtCd", label:"设备ID"},
			{name : "eqmtType", label:"设备型号", dict:'EQFA0100'},
			{name : "eqmtType", label:"设备型号", hidden : true},
			{name : "vrsnName", label:"当前模版名称", width:"200"},
			{name : "updateVerson", label:"设置模版名称", width:"200"},
			{name : "upgradeSt", label:"设置状态", 
				formatter:function(cellvalue, options, rowObject){
					switch (rowObject.upgradeSt) {
						case "UPST0800" : return "<em style='color:  #EB6361'>设置失败 </em>";
						case "UPST0900" : return "<em style='color:  #25C95B'>设置成功</em>";
						case "UPST0100" : return "<em style='color:  #25C95B'>未升级</em>";
						case "UPST0200" : return "<em style='color:  #25C95B'>升级中</em>";
						case "UPST0300" : return "<em style='color:  #25C95B'>失败</em>";
						case "UPST0400" : return "<em style='color:  #25C95B'>成功</em>";
						case "UPST0500" : return "<em style='color:  #25C95B'>取消失败</em>";
						case "UPST0600" : return "<em style='color:  #25C95B'>取消成功</em>";
						case "UPST0700" : return "<em style='color:  #25C95B'>升级请求中</em>";
						default : return rowObject.upgradeSt;
					}
			}},
			{name : "stMsg", label:"状态消息",align : "right",dict: "UPSM0000"},
			{name : "carId", label:"carId", hidden: true},
			{name : "lineId", label:"lineId", hidden: true},
			{name : "logId", label:"logId",hidden: true},
			{name : "eqmtId", label:"eqmtId", hidden: true},
			{name : "vrsnId", label:"vrsnId",hidden: true}
		],
		gridComplete:function(){
			var _t = $(this);
			var rowdata = _t.getRowData();
			for(var i=0;i<rowdata.length;i++){
				setVrsnName(i+1,function(index,selectStr){
					_t.setCell(index,"updateVerson",selectStr);
				});
			}
			rowids =[];
		}
    });
    
  //jqgrid中的对应版本
    function setVrsnName(index,callback){
    	$.ajaxExt({
    		url : "/airConditioner/getVersionName.htm",
    		 
    	}).done(function(result){
    		 var vrsnName="";
    		 var sel="<select id='jqgridUpgradeName'  class='u-input'><option value=''>请选择</option>";
     		 $(result).each(function(i,item) { 
     			sel+='<option value="'+item.vrsnId+'">'+item.vrsnName+'</option>';
    		 });
    		 sel+=" </select>";
     		 if(typeof callback === 'function'){
     			 callback(index,sel);
     		 }
     	});
    }
    
    function search(){
    	var type = commonFun.getLeftTreeCurrentTreeType();
		var nodes = commonFun.getLeftTreeCheckedNodes(type);		
		if(nodes){
			$('#grid').grid('setGridParam', {page:1,datatype: "json",postData:{"type":type,"orgIds":nodes.toString()}}).trigger("reloadGrid");
		}
		else{
			$.messager.alert("警告","	请选择查询车辆!","warning");
		}
    }
    
})
